//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.Controls.Galleries;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;
using TWC = Telligent.Web.UI;
using ResourceControl = CommunityServer.ControlPanel.Controls.ResourceControl;

namespace CommunityServer.ControlPanel.PhotoAdmin
{
	/// <summary>
	///		Summary description for SectionEditWrapper.
	/// </summary>
	public class SectionEditControl : BaseGalleryAdminControl
	{

		#region Public Properties

		private int sectionID = -1;
		public int SectionID
		{
			get { return sectionID; }
			set { sectionID = value;}
		}
		private int groupID = -1;
		public int GroupID
		{
			get { return groupID; }
			set { groupID = value;}
		}


		private Gallery gallery;
		public Gallery Gallery
		{
			get {return gallery;}
			set {gallery = value;}
		}

		#endregion

		#region Child Controls
		
		protected DirectoryWriter Directorywriter1;
		protected TWC.TabbedPanes SettingTabs;
		protected LinkButton SaveButton;

		protected SectionEditDetailsControl SectionDetails1;
		protected SectionPermissionListControl SectionPermissions1;
	
		protected SkinOptionsControl Skin1;
		protected SkinOptionsCSSControl CSS1;
		protected SkinOptionsLayoutControl Layout1;
		protected LocalQuotasControl Quotas1;

		protected Literal RegionTitle;
		protected ResourceControl RegionDescription;
		protected Script Script1;

		protected ControlPanelSelectedNavigation SelectedNavigation1;
		
		#endregion

		private void Page_Load(object sender, EventArgs e)
		{
			SectionDetails1.SectionID = SectionPermissions1.SectionID = SectionID;
			SectionDetails1.GroupID = GroupID;
			
			if(!Page.IsPostBack)
				Quotas1.Gallery = Skin1.Gallery = CSS1.Gallery = Layout1.Gallery = Gallery;

			GalleryConfiguration config = GalleryConfiguration.Instance(false); 
			SettingTabs.Panes[2].Visible = config.EnableThemes;
			SettingTabs.Panes[3].Visible = config.EnableThemes;
			SettingTabs.Panes[4].Visible = config.EnableThemes;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		public bool Save()
		{
			if(!Page.IsValid)
				return false;

			SectionDetails1.Save() ;
			if(!Page.IsValid)
				return false;
			
			SectionPermissions1.SectionID = SectionDetails1.SectionID;
			SectionPermissions1.Save();
			if(!Page.IsValid)
				return false;

			//refresh the gallery cache
			CSContext cntx = CSContext.Current;
			CommunityServer.Galleries.Galleries.GetGalleries(cntx, cntx.User.UserID, false, false, true, false);

			Gallery = CommunityServer.Galleries.Galleries.GetGallery(SectionDetails1.SectionID);

			Skin1.GetFormValues(Gallery) ;
			CSS1.GetFormValues(Gallery) ;
			Layout1.GetFormValues(Gallery) ;
			Quotas1.GetFormValues(Gallery) ;

			if(!Page.IsValid)
				return false;
	
			CommunityServer.Galleries.Galleries.Update(Gallery);

			Skin1.SetSelectedTheme(Gallery) ;

			return true;
		}

	}
}
